enum BTN {
    //% block="A"
    a,
    //% block="B"
    b
}


//% color="#ef784c" iconWidth=50 iconHeight=40
namespace Unihiker_camera_v2{
    //% block="人脸识别初始化" blockType="command"
    export function facecamerainit(parameter: any, block: any) {
        Generator.addImport(`import cv2\nimport numpy as np\nimport face_recognition`)
        Generator.addCode(`KNOW_FACE_DIR = "know_faces"`)
        Generator.addCode(`KNOW_FACE_ENCODING = []`)
        Generator.addCode(`KNOW_FACE_NAMES = []`)
    }

    //% block="人脸录入,用摄像头[CAMNUM]拍照保存为[FILENAME],备注名称为[FACENAME]" blockType="command"
    //% CAMNUM.shadow="number" CAMNUM.defl="0"
    //% FILENAME.shadow="string" FILENAME.defl="face.png"
    //% FACENAME.shadow="string" FACENAME.defl="小明"
    export function facecamerastart(parameter: any, block: any) {
        let num=parameter.CAMNUM.code;
        let filename=parameter.FILENAME.code;
        let facename=parameter.FACENAME.code;
        Generator.addImport(`\nfrom camera_face_handle import save_face_reco_handle`)
        Generator.addCode(`save_face_reco_handle(${num}, ${filename}, ${facename})`)
    }

    //% block="人脸识别,用摄像头[CAMNUM]拍照保存为[FILENAME],并获得识别结果" blockType="reporter"
	//% CAMNUM.shadow="number" NAME.defl="0"
    //% FILENAME.shadow="string" FILENAME.defl="image.png"
    export function facecheckstart(parameter: any, block: any) {
        let num=parameter.CAMNUM.code;
        let filename=parameter.FILENAME.code;
        Generator.addImport(`\nfrom camera_face_handle import check_face_reco_handle`)
        Generator.addCode(`check_face_reco_handle(${num}, ${filename})`)
    }

    //% block="视频人脸识别,初始化摄像头[CAMNUM],屏幕宽[WIDTH]高[HEIGHT]" blockType="command"
    //% CAMNUM.shadow="number" CAMNUM.defl="0"
    //% WIDTH.shadow="number" WIDTH.defl="240"
    //% HEIGHT.shadow="number" HEIGHT.defl="320"
    export function readcap(parameter: any, block: any) {
        let num=parameter.CAMNUM.code;
        let width=parameter.WIDTH.code;
        let height=parameter.HEIGHT.code;
        Generator.addImport(`import cv2\nimport numpy as np`)
        Generator.addCode(`WIDTH = ${width}`)
        Generator.addCode(`HEIGHT = ${height}`)
        Generator.addCode(`
try:
    global KNOW_FACE_ENCODING
    global KNOW_FACE_NAMES
except:
    pass
if "KNOW_FACE_ENCODING" not in globals():
    raise ValueError("KNOW_FACE_ENCODING not defined")
if "KNOW_FACE_NAMES" not in globals():
    raise ValueError("KNOW_FACE_NAMES not defined")
        `)
        Generator.addCode(`cap = cv2.VideoCapture(${num})`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)`)
        Generator.addCode(`cv2.namedWindow('cvwindow',cv2.WND_PROP_FULLSCREEN)`)
        Generator.addCode(`cv2.setWindowProperty('cvwindow', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)`)          
        Generator.addCode(`while not cap.isOpened():
    continue`)    
    }

    //% block="视频人脸识别,摄像并显示识别结果(循环中)" blockType="command"
    export function readcapcapture(parameter: any, block: any) {
        Generator.addCode(`
cvimg_success, frame = cap.read()
cvimg_h, cvimg_w, cvimg_c = frame.shape
cvimg_w1 = cvimg_h*WIDTH//HEIGHT
cvimg_x1 = (cvimg_w-cvimg_w1)//2
frame = frame[:, cvimg_x1:cvimg_x1+cvimg_w1]
frame = cv2.resize(frame, (WIDTH, HEIGHT))

rgb_frame = frame[:, :, ::-1]

face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
    matches = face_recognition.compare_faces(KNOW_FACE_ENCODING, face_encoding)
    name = ""
    if True in matches:
        first_match_index = matches.index(True)
        name = KNOW_FACE_NAMES[first_match_index]
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
    cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255), 1)
cv2.imshow('Video', frame)`)
    }

    //% block="视频人脸识别,关闭摄像头" blockType="command"
    export function closecap(parameter: any, block: any) {
        Generator.addCode(`cap.release()
cv2.destroyAllWindows() `)
    }

}
